define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yuncunchu/file/index' + location.search,
                    del_url: 'yuncunchu/file/del',
                    multi_url: 'yuncunchu/file/multi',
                    table: 'yuncunchu_file',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'original_name', title: __('原始文件名'), formatter: function(value, row, index) {
                            var icon = Controller.api.getFileIcon(row.extension);
                            return '<i class="' + icon + '"></i> ' + value;
                        }},
                        {field: 'file_size_text', title: __('文件大小')},
                        {field: 'file_type', title: __('文件类型')},
                        {field: 'config.name', title: __('存储配置')},
                        {field: 'upload_module', title: __('上传模块')},
                        {field: 'createtime', title: __('上传时间'), formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: function(value, row, index) {
                            // 操作按钮
                            var table = this.table;
                            var options = table.bootstrapTable('getOptions');
                            var buttons = [];
                            buttons.push({
                                name: 'detail',
                                text: __('Detail'),
                                title: __('Detail'),
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                icon: 'fa fa-list',
                                url: 'yuncunchu/file/detail',
                                callback: function (data) {
                                    Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                }
                            });
                            buttons.push({
                                name: 'download',
                                text: __('Download'),
                                title: __('Download'),
                                classname: 'btn btn-xs btn-success',
                                icon: 'fa fa-download',
                                url: 'yuncunchu/file/download',
                                callback: function (data) {
                                    // 下载文件
                                    window.open(data.url);
                                }
                            });
                            buttons.push({
                                name: 'del',
                                text: __('Delete'),
                                title: __('Delete'),
                                classname: 'btn btn-xs btn-danger btn-delone',
                                icon: 'fa fa-trash',
                                url: 'yuncunchu/file/del',
                                confirm: __('Are you sure you want to delete this item?'),
                                success: function (data, ret) {
                                    Layer.msg(ret.msg);
                                    return false;
                                },
                                error: function (data, ret) {
                                    Layer.msg(ret.msg);
                                    return false;
                                }
                            });
                            return Table.api.formatter.operate.call(this, value, row, index, buttons);
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            
            // 根据文件类型获取图标
            getFileIcon: function(fileType) {
                if (!fileType) return 'fa fa-file';
                
                var extension = fileType.toLowerCase();
                if (extension.indexOf('.') === 0) {
                    extension = extension.substring(1);
                }
                
                // 图片文件
                if (['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'].indexOf(extension) !== -1) {
                    return 'fa fa-file-image-o';
                }
                // 文档文件
                else if (['doc', 'docx', 'pdf', 'txt', 'rtf'].indexOf(extension) !== -1) {
                    return 'fa fa-file-text-o';
                }
                // 表格文件
                else if (['xls', 'xlsx', 'csv'].indexOf(extension) !== -1) {
                    return 'fa fa-file-excel-o';
                }
                // 演示文件
                else if (['ppt', 'pptx'].indexOf(extension) !== -1) {
                    return 'fa fa-file-powerpoint-o';
                }
                // 压缩文件
                else if (['zip', 'rar', '7z', 'tar', 'gz'].indexOf(extension) !== -1) {
                    return 'fa fa-file-archive-o';
                }
                // 音频文件
                else if (['mp3', 'wav', 'flac', 'aac', 'ogg'].indexOf(extension) !== -1) {
                    return 'fa fa-file-audio-o';
                }
                // 视频文件
                else if (['mp4', 'avi', 'mov', 'wmv', 'flv', 'webm'].indexOf(extension) !== -1) {
                    return 'fa fa-file-video-o';
                }
                // 代码文件
                else if (['js', 'css', 'html', 'php', 'py', 'java', 'cpp', 'c'].indexOf(extension) !== -1) {
                    return 'fa fa-file-code-o';
                }
                // 默认文件图标
                else {
                    return 'fa fa-file-o';
                }
            }
        }
    };
    return Controller;
});

